package com.dzzw.Dao;

import com.dzzw.Model.Attendance;
import com.dzzw.utils.DButils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

//考勤
public class AttendanceDao {
    private final Connection con = DButils.getCon();
    //签到
    public void SignIn(Attendance attendance) throws ParseException {
        String attendance_status = null;
        Date attendance_time = new Date();
        SimpleDateFormat SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        
        Date date1 = new SimpleDateFormat("HH:mm:ss").parse("HH:mm:ss");
        Date date2 = new SimpleDateFormat("HH:mm:ss").parse("10:00:00");
        
        if(date1.getTime()<=date2.getTime()){
            attendance_status = "正常";
        }else {
            attendance_status = "迟到";
        }
        String sql = "insert into t_attendance(employee_id,attendance_status,attendance_time) values(?,?,?)";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,attendance.getAttendance_id());
            preparedStatement.setString(2,attendance_status);
            preparedStatement.setDate(3,java.sql.Date.valueOf(SimpleDateFormat.format(attendance_time.getTime())));
            preparedStatement.executeUpdate();
        }catch (Exception e){
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //签退
    public void SignOut(Attendance attendance) throws ParseException {
        String attendance_status;
        Date attendance_time = new Date();
        SimpleDateFormat SimpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        Date date1 = new SimpleDateFormat("HH:mm:ss").parse("HH:mm:ss");
        Date date2 = new SimpleDateFormat("HH:mm:ss").parse("17:00:00");

        if(date1.getTime()>=date2.getTime()){
            attendance_status = "正常";
        }else {
            attendance_status = "早退";
        }
        String sql = "insert into t_attendance(employee_id,attendance_status,attendance_time) values(?,?,?)";
        try{
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,attendance.getEmployee_id());
            preparedStatement.setString(2,attendance_status);
            preparedStatement.setDate(3,java.sql.Date.valueOf(SimpleDateFormat.format(attendance_time.getTime())));
            preparedStatement.executeUpdate();
        }catch (Exception e){
            System.out.println("数据添加失败！");

            e.printStackTrace();
        }
    }

    //根据公务员id查询个人打卡记录
    public static List<Attendance> SelectAllAttendanceByEmployeeId(int employee_id){
        List<Attendance> list = new ArrayList<>();
        String sql = "SELECT a.attendance_id,e.employee_name,a.attendance_status,a.attendance_time FROM t_attendance AS a INNER JOIN t_employee AS e ON a.employee_id = e.employee_id where a.employee_id = ?";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,employee_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Attendance attendance = new Attendance();
                attendance.setAttendance_id(resultSet.getInt(1));
                attendance.setEmployee_name(resultSet.getString(2));
                attendance.setAttendance_status(resultSet.getString(3));
                attendance.setAttendance_time(resultSet.getDate(4));
                list.add(attendance);
            }
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }

    //根据部门id查询部门员工打卡记录
    public static List<Attendance> SelectAllByDepartmentId(int leader_id){
        List<Attendance> list = new ArrayList<>();
        String sql = "SELECT a.attendance_id,e.employee_name,a.attendance_status,a.attendance_time,d.department_name FROM t_attendance AS a INNER JOIN t_employee AS e ON a.employee_id = e.employee_id INNER JOIN t_department AS d ON e.department_id = d.department_id  where e.leader_id = ?";
        try{
            Connection con = DButils.getCon();
            PreparedStatement preparedStatement = con.prepareStatement(sql);
            preparedStatement.setInt(1,leader_id);
            ResultSet resultSet = preparedStatement.executeQuery();
            while (resultSet.next()){
                Attendance attendance = new Attendance();
                attendance.setAttendance_id(resultSet.getInt(1));
                attendance.setEmployee_name(resultSet.getString(2));
                attendance.setAttendance_status(resultSet.getString(3));
                attendance.setAttendance_time(resultSet.getDate(4));
                attendance.setDepartment_name(resultSet.getString(5));
                list.add(attendance);
            }
            preparedStatement.close();
            con.close();
        }catch (Exception e){
            e.printStackTrace();
        }
        return list;
    }
}
